using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameData;

namespace Project1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        SpriteBatch spriteBatch;
        PlayerShip player;
        Level[] levels = null;
        int currentLevel = 0;
        LevelData[] leveldat;
        Powerup[] levelPowerupData;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsFixedTimeStep = false;
            graphics.PreferredBackBufferWidth = GlobalState.Screen_Width;
            graphics.PreferredBackBufferHeight = GlobalState.Screen_Height;
            graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            //base.LoadContent();
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = new PlayerShip(Content.Load<Texture2D>("gradius"), Content.Load<Texture2D>("Bullet-Bill"));
            levelPowerupData = new Powerup[3];
            for (int i = 0; i < 3; i++)
            {
                string s = "Powerup" + Convert.ToString(i + 1);
                levelPowerupData[i] = new Powerup(Content.Load<Texture2D>(s), i + 1, new Vector2(800, 100 + i * 50), 1);

            }

            levels = new Level[1];
            leveldat = Content.Load<LevelData[]>("Level1");
            levels[0] = new Level( Content.Load<Texture2D>("bg"), new Vector2( 800.0f, 600.0f ));
            levels[0].createPowerups(levelPowerupData);
            levels[0].createEnemy( Content, leveldat );
            levels[0].start();


            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            player.update(gameTime.ElapsedGameTime.TotalSeconds);
            levels[currentLevel].update(gameTime.ElapsedGameTime.TotalSeconds, player);
            if (levels[currentLevel].spawnedBoss && !levels[currentLevel].levelBoss.alive)
            {
                levels[currentLevel].active = false;
                currentLevel++;
                levels[currentLevel].active = true;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            levels[currentLevel].draw(spriteBatch);
            player.draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
